//
//  WXGAlertView.m
//  WXGAlertView-Demo
//
//  Created by Mac on 15/8/12.
//  Copyright (c) 2015年 BV-3G. All rights reserved.
//

#import "WXGAlertView.h"

@implementation WXGAlertView

#define kAlertViewWidth self.frame.size.width
#define kAlertViewHeight self.frame.size.height


- (void)getButtnIndex:(AlertBolck)block {

    _alertBlock = block;


}

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        
        [self reloadBounce];
        /*重置self的尺寸
         *获取程序的主窗口 设置视图处在屏幕的中央
         */
        
        [self creatSubViews];
        /*添加基本控件：
         *contentview:用来放置外部添加的视图
         *两个button：关闭按钮／详情按钮
         */
    }

    return self;
}

- (void)reloadBounce {

    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    
    self.center = window.center;

    self.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.layer.borderWidth = 5;
    self.layer.cornerRadius = 5;
    
    self.clipsToBounds = YES;
    
}



- (void)creatSubViews {

    _contentView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, kAlertViewWidth -10, kAlertViewHeight -50 -10)];
    
    _contentView.backgroundColor = [UIColor redColor];
    
    [self addSubview:_contentView];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn1.frame = CGRectMake(5, _contentView.frame.size.height +5,  (kAlertViewWidth -10)/2, 50);
    btn1.tag = 200 +0;
    [btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn1 setTitle:@"关闭" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn2.frame = CGRectMake(5+(kAlertViewWidth -10)/2 , _contentView.frame.size.height +5,  (kAlertViewWidth -10)/2, 50);
    btn2.tag = 200+1;
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"查看详情" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn2];

}

/*
 *将self添加到窗口上
 *完成了两层动画  实现动态的弹出效果
 */
- (void)show {

    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    
    [window addSubview:self];
    
    self.transform = CGAffineTransformMakeScale(.5, .5);
    
    [UIView animateWithDuration:.2 animations:^{
        
        self.transform = CGAffineTransformMakeScale(1.2, 1.2);
        
    } completion:^(BOOL finished) {
       
        [UIView animateWithDuration:.2 animations:^{
           
            self.transform = CGAffineTransformIdentity;
        }];
        
    }];

}

//点击关闭按钮时调用 将self从窗口上移除
- (void)hidden {

    [UIView animateWithDuration:.35 animations:^{
        
        self.alpha = 0;
        
        self.transform = CGAffineTransformMakeScale(3, 3);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];

}

//按钮调用的方法  给block传值（点击的按钮在self上的位置）
- (void)buttonAction:(UIButton *)sender {

    if (sender.tag == 200) {
        
        [self hidden];
    }
    
    if (_alertBlock) {
    
        _alertBlock(sender.tag -200);
    }

}

@end
